package com.bankLogisticsSys.controller;

import java.sql.Date;
import java.util.Set;

import com.bankLogisticsSys.interceptor.ActionInterceptor;
import com.bankLogisticsSys.model.Expressinfo;
import com.bankLogisticsSys.model.Userinfo;
import com.bankLogisticsSys.service.ExpressService;
import com.bankLogisticsSys.service.LogisticsService;
import com.bankLogisticsSys.service.TransformService;
import com.bankLogisticsSys.util.WeixinUtil;
import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.plugin.redis.Cache;
import com.jfinal.plugin.redis.Redis;
import com.jfinal.weixin.sdk.api.ApiConfigKit;
import com.jfinal.weixin.sdk.api.SnsAccessToken;
import com.jfinal.weixin.sdk.api.SnsAccessTokenApi;

@Before(ActionInterceptor.class)
public class BaseController extends Controller{
	
	public static final int GET_ACTION = 0;
	public static final int SEND_ACTION = 1;
	public static final int RECEIVE_ACTION = 2;
	public static final int PACK_ACTION = 3;
	public static final int ACCEPT_ACTION = 4;
	
	public static final int POST_MAN = 5;
	
	public static final String TRACKINGID = "trackingID";
	public static final String SEQUENCE = "sequence";
	public static final String ACTION = "action";
	public static final String ABNORMAL = "abnormal";
	public static final String NEXT_NETWORK = "nextNetwork";
	
	//快递单的状态 验收/运输/拒收
	public static final int ACCEPT = 0;
	public static final int TRANSPORT = 1;
	public static final int REJECT = 2;
	
	//快递单的异常情况
	public static final int IS_NORMAL = 0;
	
	public static final boolean OAUTH2_OPEN = true;
	
	public String openId = WeixinMsgController.openId;
	public Cache actionCache = Redis.use();
	public ExpressService exservice = new ExpressService();
	public LogisticsService ls = new LogisticsService();
	
	public String OauthOpenId(String code){
		String appId  = WeixinUtil.getApiConfig().getAppId();
		String secret = WeixinUtil.getApiConfig().getAppSecret();
		SnsAccessToken snsAccessToken = SnsAccessTokenApi.getSnsAccessToken(appId, secret, code);
		
		return snsAccessToken.getOpenid();	
	}
	public int getAbnormal(long trackingID){
		return actionCache.hget(String.valueOf(trackingID), ABNORMAL);
	}
	public int getSequence(long trackingID){
		return actionCache.hget(String.valueOf(trackingID), SEQUENCE);
	}
	public int getNextNetwork(String openId){
		int nextNetwork = 0;
		if(actionCache.hget(openId, NEXT_NETWORK)!=null){
			return actionCache.hget(openId, NEXT_NETWORK);
		}else{
			return nextNetwork;
		}
	}
	@SuppressWarnings("unchecked")
	public Set<String> getTrackingIDSet(){
		return actionCache.smembers(openId +"List");
	}
	public Boolean updateAcceptExpressState(long trackingID){
		Expressinfo ei = exservice.getInfoByTrackingID(trackingID);
		java.sql.Date receiveDate = new java.sql.Date(System.currentTimeMillis());
		ei.setState(ACCEPT);
		ei.setReceiveDate(receiveDate);
		return ei.update();
	}

	//isAccept 用于标记验收操作
	public Boolean dealWholeProcess(Set<String> trackingIDSet,int action, Boolean isAccept){
		long trackingID = 0;
		int sequence = 0;
		int abnormal = 0;		

		for (String temp : trackingIDSet) {  
			trackingID = Long.parseLong(temp);
			sequence = getSequence(trackingID);
			abnormal = getAbnormal(trackingID);
			if(ls.dealProcess(trackingID, sequence, abnormal, action, openId)){
				if(isAccept){
					updateAcceptExpressState(trackingID);
				}
				actionCache.del(String.valueOf(trackingID));
			}
		}
		return true;
	}
	
	public Boolean sendProcessMsg(int trackingIDNum, int action, int nextNetwork,String openId){
		ApiConfigKit.setThreadLocalApiConfig(WeixinUtil.getApiConfig());
		WeixinApiController wac = new WeixinApiController();
		String msg = TransformService.getAction(action) + "操作完成\n共处理 " + trackingIDNum +" 个银行快递";
		System.out.println("msg------" + msg);
		System.out.println(nextNetwork);
		if(nextNetwork!=0){
			msg += "\n下一站：" + TransformService.getNetwork(nextNetwork);
			System.out.println("msg++" + msg);
			Userinfo user1 = exservice.getUserinfo(openId);
			Userinfo user = exservice.getUserinfoByNetworkID(nextNetwork);
			if(user!=null && user1.getRole()!= POST_MAN ){
				String nextStationMsg;
				if(action == PACK_ACTION){
					nextStationMsg = TransformService.getNetwork(user1.getNetworkID()) + 
							"已封包" + trackingIDNum + "单银行快递,请分配揽投员前去揽收";
				}else{
					nextStationMsg = TransformService.getNetwork(user1.getNetworkID()) + 
							"已发出" + trackingIDNum + "单银行快递，请注意查收";
				}
				wac.sendProcessMsg(user.getOpenID(), nextStationMsg);
			}
		}
		wac.sendProcessMsg(openId, msg);
		return true;
	}
	
	public String getLogString(String openId, String action,Set<String> trackingIDSet){
		Userinfo user = exservice.getUserinfo(openId);
		String log = "登录";
		if(user!=null){
		log =  "\n用户:  " + user.getEmployeeID()
				+ "\n操作: " + action
				+ "\n单号: " + trackingIDSet + "\n";
		}
		return log;
	}
	
}
